package io.esirong.widget.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import java.lang.ref.WeakReference;

import io.esirong.widget.R;

/**
 * 简一弹窗：只作显示信息<Br>
 * <p>
 * 与用户的交互仅限于确认消息（交互性强于Toast)
 *
 * @author Created by 黄嵘才(HuangRc)
 */
public class MsgConfirmDialog {

    private WeakReference<Activity> activityRef;

    private static MsgConfirmDialog instance;

    private WeakReference<Dialog> dialogRef;

    private int resLayout = R.layout.dialog_layout;

    private MsgConfirmDialog(Activity context) {
        activityRef = new WeakReference<>(context);
    }

    public static MsgConfirmDialog instance(final Activity context) {
        //显示一个在APP活动时
        if (instance != null && instance.activityRef != null) {
            if (instance.activityRef.get() == context) {
                return instance;
            } else {
                if (instance.isShowing()) {
                    instance.dismiss();
                }
                instance = null;
            }
        }

        if (instance == null) {
            synchronized (MsgConfirmDialog.class) {
                if (instance == null) {
                    instance = new MsgConfirmDialog(context);
                }
            }
        }

        return instance;
    }


    private Dialog createDialog(final Activity context) {
        Dialog dialog = onCreateDialog(context);
        if (dialog == null) {
            dialog = createDefault(context);
        }

        return dialog;
    }

    private Dialog createDefault(final Activity context) {
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(resLayout);
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialog.getWindow().setAttributes(lp);
        //自定义背景颜色
        dialog.getWindow().getDecorView().setBackgroundResource(0x00000000);

        Button button = dialog.findViewById(R.id.btn_dialog_button_yes);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        return dialog;
    }

    protected Dialog onCreateDialog(final Activity context) {
        return null;
    }

    public void show(final String msg) {
        if (dialogRef == null || dialogRef.get() == null) {
            dialogRef = new WeakReference<>(createDialog(activityRef.get()));
        }
        if (!isShowing()) {
            /* 如果Activity被回收了，则不要显示了 */
            Activity activity = activityRef.get();
            if (!(activity == null || activity.isFinishing())) {

                TextView textView = dialogRef.get().findViewById(R.id.dialog_text);
                if (textView != null) {
                    textView.setText(msg);
                }

                dialogRef.get().show();
            }
        }
    }

    public void show() {
        show("");
    }

    public void dismiss() {
        if (dialogRef == null) {
            return;
        }
        if (dialogRef.get() != null) {
            dialogRef.get().dismiss();
        }
    }

    public boolean isShowing() {
        if (dialogRef == null) {
            return false;
        }
        if (dialogRef.get() != null) {
            return dialogRef.get().isShowing();
        }
        return false;
    }
}
